package com.xf.zk.curator.example.znode;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.imps.DefaultACLProvider;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;

/**
 * https://blog.csdn.net/qq_33513250/article/details/102677481
 */
public class CreateNode {
    public static void main(String[] args) throws Exception {
        String path1 = "/curator-create/c1";
        // 创建CuratorFramework 客户端实例，集群服务器地址为127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183，session timeout5000ms
        // 回调策略为ExponentialBackoffRetry，即为retries 3 times with increasing 1000 sleep time between retries
        CuratorFramework client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183").sessionTimeoutMs(5000)
                        .retryPolicy(new ExponentialBackoffRetry(1000, 3)).aclProvider(new DefaultACLProvider()).build();
        // 开始连接
        client.start();
        // 创建 EPHEMERAL类型节点，并创建父节点
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path1, "init".getBytes());
        System.out.println("success create znode: " + path1);
        String path2 = "/curator-create/c2";
        // 创建 EPHEMERAL类型节点，并创建父节点,设置acl权限控制
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(path2, "init2".getBytes());
        System.out.println("success create znode: " + path2);
    }
}
